//
//  CQCardMatchView.swift
//  TSListDemo
//
//  Created by 李超前 on 2020/11/22.
//  Copyright © 2020 dvlproad. All rights reserved.
//

import Foundation
import UIKit
import SnapKit
import CJBaseUIKit

class CQCardMatchView: UIView {
    var matchLabel1: UILabel?
    var matchLabel2: UILabel?
    
    var continueBlock: (()->())?
    var chatBlock:  (()->())?
    
    // MARK: - Setter
    // 今日喜欢次数
    private var _matchUser :CQUserMatchModel!
    var matchUser: CQUserMatchModel {
        set {
            self._matchUser = newValue
            
            self.matchLabel1?.text = String.init(format: "你和 %@ 相互喜欢了对方", matchUser.name)
            
            let matchYourTypeString: String = matchUser.matchYourTypeString()
            let likeString: String = String.init(format: "%@喜欢了你的“%@”", matchUser.sexFlagString(), matchYourTypeString)
            let attributedString: NSMutableAttributedString = NSMutableAttributedString.init(string: likeString)
            
            let likeNSSting = NSString.init(string: likeString)
            let matchTypeRange: NSRange = likeNSSting.range(of: matchYourTypeString)
            let matchTypeAttributes: [NSAttributedString.Key : Any] =
                [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 30),
                 NSAttributedString.Key.foregroundColor: #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)]
            attributedString.addAttributes(matchTypeAttributes, range: matchTypeRange)
            self.matchLabel2?.attributedText = attributedString
            
        }
        get {
            return self._matchUser
        }
    }
    
    // MARK: - Init
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = #colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1)
        
        let topLabel: UILabel = UILabel.init(frame: .zero)
        topLabel.textAlignment = NSTextAlignment.center
        topLabel.textColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        topLabel.font = UIFont.systemFont(ofSize: 40)
        topLabel.text = NSLocalizedString("Match", comment: "Match")
        self.addSubview(topLabel)
        topLabel.snp_makeConstraints { (make) in
            make.left.equalToSuperview().offset(20)
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(40)
            make.height.equalTo(60)
        }
        
        let matchLabel1: UILabel = UILabel.init(frame: .zero)
        matchLabel1.textAlignment = NSTextAlignment.center
        matchLabel1.textColor = UIColor.cjColor(withHexString: "#FFFFFF", alpha: 0.5)
        matchLabel1.font = UIFont.systemFont(ofSize: 14)
        self.addSubview(matchLabel1)
        matchLabel1.snp_makeConstraints { (make) in
            make.left.equalToSuperview().offset(20)
            make.centerX.equalToSuperview()
            make.centerY.equalTo(self).offset(60)
            make.height.equalTo(20)
        }
        self.matchLabel1 = matchLabel1
        
        let matchLabel2: UILabel = UILabel.init(frame: .zero)
        matchLabel2.textAlignment = NSTextAlignment.center
        matchLabel2.textColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        matchLabel2.font = UIFont.systemFont(ofSize: 14)
        self.addSubview(matchLabel2)
        matchLabel2.snp_makeConstraints { (make) in
            make.left.equalToSuperview().offset(20)
            make.centerX.equalToSuperview()
            make.top.equalTo(matchLabel1.snp_bottom).offset(3)
            make.height.equalTo(42)
        }
        self.matchLabel2 = matchLabel2
        
        
        let continueButton: UIButton = UIButton.init(type: UIButton.ButtonType.custom)
        continueButton.backgroundColor = UIColor.clear
        continueButton.setTitle(NSLocalizedString("继续探索 >", comment: "继续探索 >"), for: .normal)
        continueButton.setTitleColor(#colorLiteral(red: 1, green: 1, blue: 1, alpha: 1), for: .normal)
        continueButton.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        continueButton.addTarget(self, action: #selector(continueAction), for: UIControl.Event.touchUpInside)
        self.addSubview(continueButton)
        continueButton.snp_makeConstraints { (make) in
            make.left.equalTo(self).offset(50)
            make.centerX.equalTo(self)
            make.bottom.equalTo(self).offset(-40)
            make.height.equalTo(20)
        }
        
        let chatButton: UIButton = UIButton.init(type: UIButton.ButtonType.custom)
//        chatButton.backgroundColor = #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)
        let gradientLayer = CAGradientLayer()
        gradientLayer.cornerRadius = 25
        gradientLayer.colors = [#colorLiteral(red: 1, green: 0.3058823529, blue: 0.9294117647, alpha: 1).cgColor,#colorLiteral(red: 0.231372549, green: 0.2745098039, blue: 0.9647058824, alpha: 1).cgColor]
        gradientLayer.locations = [0.0, 1.0]
        gradientLayer.startPoint = CGPoint.init(x: 0, y: 0)
        gradientLayer.endPoint = CGPoint.init(x: 1, y: 0)
        gradientLayer.frame = chatButton.bounds
        let chatButtonWidth = self.frame.size.width - 2*50
        gradientLayer.frame = CGRect.init(x: 0, y: 0, width: chatButtonWidth, height: 50)
        chatButton.layer.addSublayer(gradientLayer)
        
        chatButton.setTitle(NSLocalizedString("开始聊天", comment: "开始聊天"), for: .normal)
        chatButton.setTitleColor(#colorLiteral(red: 1, green: 1, blue: 1, alpha: 1), for: .normal)
        chatButton.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        chatButton.addTarget(self, action: #selector(chatAction), for: UIControl.Event.touchUpInside)
        chatButton.layer.cornerRadius = 25
        self.addSubview(chatButton)
        chatButton.snp_makeConstraints { (make) in
            make.left.equalTo(self).offset(50)
            make.centerX.equalTo(self)
            make.bottom.equalTo(continueButton.snp_top).offset(-20)
            make.height.equalTo(50)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // MARK: - Event
    @objc fileprivate func chatAction() {
        if self.chatBlock != nil {
            self.chatBlock!()
        }
    }
    
    @objc fileprivate func continueAction() {
        if self.continueBlock != nil {
            self.continueBlock!()
        }
    }
}
